package com.chaos.meta.config;

import org.springframework.context.ApplicationListener;
import org.springframework.security.core.session.SessionDestroyedEvent;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;

/**
 * @author naicha
 * 单例化session管理器, 可对SessionRegistryImpl方法进行重写，实现分布式session管理。
 */
// TODO：使用设计模式应在命名进行体现
public class CustomSessionRegistry extends SessionRegistryImpl implements
		SessionRegistry, ApplicationListener<SessionDestroyedEvent> {

	volatile private static CustomSessionRegistry instance = null;

	private CustomSessionRegistry(){
		
	}

	public static CustomSessionRegistry getInstance()
	{
		// 双检查锁机制，避免同步导致的低并发度
		if(instance == null)
		{
			synchronized (CustomSessionRegistry.class){
				if (instance == null)
				{
					instance = new CustomSessionRegistry();
				}
			}
		}
		return instance;
	}
}
